package com.surfacing.rcp.model.codec;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;


import org.apache.log4j.Logger;

import com.surfacing.rcp.model.common.AbstractMessage;
import com.surfacing.rcp.model.entity.Anforderung;
import com.surfacing.rcp.model.entity.Dienststelle;
import com.surfacing.rcp.model.entity.Doctor;
import com.surfacing.rcp.model.entity.Einsatzart;
import com.surfacing.rcp.model.entity.Journal;
import com.surfacing.rcp.model.entity.Naca;
import com.surfacing.rcp.model.entity.Nfs;
import com.surfacing.rcp.model.entity.Transportziel;
import com.surfacing.rcp.model.factory.ProtocolCodecFactory;

public class JournalEncoder implements MessageEncoder {
	private static final Logger log = Logger.getLogger(JournalEncoder.class);

	@Override
	public void doEncode(AbstractMessage message, XMLStreamWriter writer)
			throws XMLStreamException {
		// Cast the object to a item
		Journal member = (Journal) message;

		// assert valid
		if (member == null) {
			log.error("WARNING: Object journal is null and cannot be encoded");
			return;
		}

		// start
		writer.writeStartElement(Journal.ID);

		// write the elements and attributes
		writer.writeStartElement("journalId");
		writer.writeCharacters(String.valueOf(member.getId()));
		writer.writeEndElement();

		writer.writeStartElement("protokollnummer");
		writer.writeCharacters(String.valueOf(member.getProtokollnummer()));
		writer.writeEndElement();

		writer.writeStartElement("datum");
		writer.writeCharacters(member.getDatum().toString());
		writer.writeEndElement();

		writer.writeStartElement("name");
		writer.writeCharacters(member.getName());
		writer.writeEndElement();
		// get the encoder for the location
		if (member.getTransportziel() != null) {
			MessageEncoder encoder = ProtocolCodecFactory.getDefault()
					.getEncoder(Transportziel.ID);
			encoder.doEncode(member.getTransportziel(), writer);
		}
		// write the elements and attributes
		writer.writeStartElement("alarmzeit");
		writer.writeCharacters(String.valueOf(member.getAlarmZeit()));
		writer.writeEndElement();
		// write the elements and attributes
		writer.writeStartElement("endzeit");
		writer.writeCharacters(String.valueOf(member.getEndZeit()));
		writer.writeEndElement();
		// write the elements and attributes
		writer.writeStartElement("created");
		writer.writeCharacters(member.getCreated().toString());
		writer.writeEndElement();
		// write the elements and attributes
		writer.writeStartElement("changed");
		writer.writeCharacters(member.getChanged().toString());
		writer.writeEndElement();
		// write the elements and attributes
		if (member.getNotfallort() != null) {
			writer.writeStartElement("notfallort");
			writer.writeCharacters(member.getNotfallort());
			writer.writeEndElement();
		}
		// write the elements and attributes
		if (member.getKm() != null) {
			writer.writeStartElement("km");
			writer.writeCharacters(member.getKm());
			writer.writeEndElement();
		}
		if (member.getNachforderung() != null) {
			MessageEncoder encoder = ProtocolCodecFactory.getDefault()
					.getEncoder(Anforderung.ID);
			encoder.doEncode(member.getNachforderung(), writer);
		}
		if (member.getEinsatzart() != null) {
			MessageEncoder encoder = ProtocolCodecFactory.getDefault()
					.getEncoder(Einsatzart.ID);
			encoder.doEncode(member.getEinsatzart(), writer);
		}
		if (member.getDienststelle() != null) {
			MessageEncoder encoder = ProtocolCodecFactory.getDefault()
					.getEncoder(Dienststelle.ID);
			encoder.doEncode(member.getDienststelle(), writer);
		}
		// write the elements and attributes
		if (member.getAzubi() != null) {
			writer.writeStartElement("azubi");
			writer.writeCharacters(member.getAzubi());
			writer.writeEndElement();
		}
		if (member.getNfs() != null) {
			MessageEncoder encoder = ProtocolCodecFactory.getDefault()
					.getEncoder(Nfs.ID);
			encoder.doEncode(member.getNfs(), writer);
		}
		if (member.getDoctor() != null) {
			MessageEncoder encoder = ProtocolCodecFactory.getDefault()
					.getEncoder(Doctor.ID);
			encoder.doEncode(member.getDoctor(), writer);
		}
		if (member.getNaca() != null) {
			MessageEncoder encoder = ProtocolCodecFactory.getDefault()
					.getEncoder(Naca.ID);
			encoder.doEncode(member.getNaca(), writer);
		}
		// end
		writer.writeEndElement();
	}

}
